//
//  QZHNavgationController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QZHNavgationController.h"
#import "UIBarButtonItem+Add.h"
@interface QZHNavgationController ()

@end

@implementation QZHNavgationController
+ (void)initialize {
    // 设置为不透明
    [[UINavigationBar appearance] setTranslucent:NO];
    
    // 拿到整个导航控制器的外观
    UIBarButtonItem * item = [UIBarButtonItem appearance];
    item.tintColor = [UIColor navagationBarColor];
    // 设置字典的字体大小
    NSMutableDictionary * atts = [NSMutableDictionary dictionary];
    
    atts[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    atts[NSForegroundColorAttributeName] = [UIColor whiteColor];
    
    // 将字典给item
    [item setTitleTextAttributes:atts forState:UIControlStateNormal];
    
    
    [[UINavigationBar appearance] setShadowImage:[UIImage new]];
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.viewControllers.count > 0) {
        
        /**
         *  如果在堆栈控制器数量大于1 加返回按钮
         */
        if (self.viewControllers.count > 0) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.highlighted = NO;
            btn.frame = CGRectMake(0, 0, 44, 44);
            if ([self.userType isEqualToString:@"100"]) {
                [btn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateHighlighted];
            } else {
                [btn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateHighlighted];
            }
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
            [btn addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            space.width = -13;
            viewController.navigationItem.leftBarButtonItems = @[space,leftItem];
        } else {
//            viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"" tintColor:[UIColor colorWithRed:0.42f green:0.33f blue:0.27f alpha:1.00f] touchBlock:nil];
        }
    }
    if ((self.viewControllers.count > 0)&&(viewController!= self.viewControllers[0])) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
        // 获取系统自带滑动手势的target对象
//        id target = self.interactivePopGestureRecognizer.delegate;
//        // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
//        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
//        // 设置手势代理，拦截手势触发
//        pan.delegate = self;
//        // 给导航控制器的view添加全屏滑动手势
//        [self.view addGestureRecognizer:pan];
        // 禁止使用系统自带的滑动手势
        self.interactivePopGestureRecognizer.enabled = YES;

    // Do any additional setup after loading the view.
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器


    if (self.childViewControllers.count == 1) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }

    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
